package com.interview.controller;

import com.interview.common.Result;
import com.interview.common.ResultMessage;
import com.interview.common.StatusCode;
import com.interview.entity.Examee;
import com.interview.entity.GroupInfo;
import com.interview.entity.WaitingRoomInfo;
import com.interview.service.ExameeService;
import com.interview.service.GroupInfoService;
import com.interview.service.WaitingRoomInfoService;
import com.interview.utils.RegexValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("waitingRoomInfo")
public class WaitingRoomInfoController {

    @Autowired
    private WaitingRoomInfoService waitingRoomInfoService;
    @Autowired
    private GroupInfoService groupInfoService;
    @Autowired
    private ExameeService exameeService;

    @PostMapping("getOne")
    public Result getOne() {
        WaitingRoomInfo oneWaitingRoomInfo = waitingRoomInfoService.getOneWaitingRoomInfo();
        if (oneWaitingRoomInfo != null) {
            return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS, oneWaitingRoomInfo);
        }else {
            return new Result(false, StatusCode.ERROR, ResultMessage.OPERATION_FAILED);
        }
    }

    @Transactional
    @PostMapping("saveOne")
    public Result saveOne(WaitingRoomInfo waitingRoomInfo) throws IOException {
        String groupIds = waitingRoomInfo.getGroupIds();
        boolean valid = RegexValidation.isValid("^[A-Z](,[A-Z])*$", groupIds);
        if (!valid) {
            return new Result(false, StatusCode.ERROR, ResultMessage.OPERATION_FAILED,"请按正确格式填写考生分组号");
        }else {
           boolean exist= waitingRoomInfoService.checkDataExist();
           if (exist){
               return new Result(false,StatusCode.ERROR,ResultMessage.OPERATION_FAILED,"数据已经存在");
           }else {
               int number= waitingRoomInfoService.addWaitingRoomInfo(waitingRoomInfo);

               //下载分组数据
               List<GroupInfo> groupInfoByIdsFromDataCenter = groupInfoService.getGroupInfoByIdsFromDataCenter(groupIds);
               //插入到数据库中
               int i = groupInfoService.addGroupInfos(groupInfoByIdsFromDataCenter);
               //下载考生数据
               List<Examee> exameeList=exameeService.getExameeInfoByIdsFromDataCenter(groupIds);
               //插入到数据库中
               int j=exameeService.addExameeInfos(exameeList);

               if (number>0){
                   return new Result(true,StatusCode.OK,ResultMessage.OPERATION_SUCCESS,number);
               }else {
                   return new Result(false,StatusCode.ERROR,ResultMessage.OPERATION_FAILED);
               }
           }
        }
    }
}
